package com.isnotlocus.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.isnotlocus.gulimail.product.entity.BrandEntity;
import com.isnotlocus.gulimail.product.entity.CategoryEntity;
import com.isnotlocus.gulimail.product.service.BrandService;
import com.isnotlocus.gulimail.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.isnotlocus.gulimail.product.entity.CategoryBrandRelationEntity;
import com.isnotlocus.gulimail.product.service.CategoryBrandRelationService;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author IsnotLocus
 * @email sunlightcs@gmail.com
 * @date 2020-07-16 17:00:57
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping("/{catelog}/list")
    public R categoryList(@RequestParam Map<String, Object> map, @PathVariable("catelog") String TypeValue) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>();
        if (TypeValue.equalsIgnoreCase("catelog")) {
            Long brandId = Long.valueOf(map.get("brandId").toString());
            if (brandId != null) {
                wrapper.eq("brand_id", brandId);
            }
        } else if (TypeValue.equalsIgnoreCase("brands")) {
            Long catId = Long.valueOf(map.get("catId").toString());
            if (catId != null) {
                wrapper.eq("catelog_id", catId);
            }
        }
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(wrapper);
        System.out.println("list,,," + list);
        return R.ok().put("data", list);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("data", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long CatelogId = categoryBrandRelation.getCatelogId();
        System.out.println("savesavesavesave" + brandId + "," + CatelogId);
        CategoryEntity CategoryEntity = categoryService.getById(CatelogId);
        BrandEntity brandEntity = brandService.getById(brandId);
        System.out.println("CategoryEntity" + CategoryEntity + "," + brandEntity);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(CategoryEntity.getName());
        categoryBrandRelationService.save(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
